
const db = wx.cloud.database()
const collectionName = 'prize'
const prizeJoin = 'prizeJoin'
const prizeWin = 'prizeWin'

export default {
  getAllPrize: function(params){
    
    return new Promise((resolve, reject) => {
        
        var skip = (params.pageNum - 1) * params.pageSize
        var limit = params.pageSize
        delete params.pageNum
        delete params.pageSize
     
        if(params.content){
          params.content = db.RegExp({
            regexp: params.content,
            options: 'im',
          })
        }

        db.collection(collectionName).aggregate()
        .match(params)
        .sort({
          top: -1,
          createDate: -1,
           
        })
        .skip(skip)
        
        .limit(limit)
        .end()
        .then(res => {
          resolve(res.list)
        })
    })
  },
  addPrize:function(data){
    return new Promise((resolve,reject) => {
      
      data.createDate = new Date()

      db.collection(collectionName).add({
        data: data
      })
      .then(res => {
        console.log(res)
        resolve(res._id)
      })
      .catch(console.error)
    })
  },

  updatePrize: async function(data){
    var id = data.id
    delete data.id

    data.createDate = new Date()

    var res = await db.collection(collectionName).doc(id).update({
      data: data
    })
    return res
  },

  deletePrize: async function(id){
    var res = await db.collection(collectionName).doc(id).remove()
    return res
  },
  
  getPrize: async function(_id){

    var res = await db.collection(collectionName).doc(_id).get()
    return res.data
  },

  async joinPrize (data){
    data.createDate = new Date()
    let res = await db.collection(prizeJoin).add({
      data: data
    })
    return res
  },
  async getIsJoin({openid, prizeId}){
    let res = await db.collection(prizeJoin).aggregate()
    .match({
      openid, prizeId
    })
    .end()
    return res.list
  },
  async getPersonCount({prizeId}){
    let res = await db.collection(prizeJoin).where({
      prizeId
    }).count()
    return res.total
  },
  async getPersonList({prizeId, pageNum, pageSize}){
    var skip = (pageNum - 1) * pageSize
    var limit = pageSize
    let res = await db.collection(prizeJoin).aggregate()
    .match({
      prizeId
    })
    .sort({
      createDate: -1
    })
    .skip(skip)
    
    .limit(limit)
    .end()
    return res.list
  },
  async getWinner({prizeId}){
        let res = await db.collection('prizeWin').aggregate()
        .match({
            prizeId: prizeId
        })
        .end()
        return res.list
  },

  async openPrize(data){
    data.createDate = new Date()
    let res = await db.collection(prizeWin).add({
      data: data
    })
    await db.collection(prizeJoin).add({
      data: data
    })
    return res
  },
  async addJoin(data){
    data.createDate = new Date()
    await db.collection(prizeJoin).add({
      data: data
    })
  },
  async getMyJoin(params){
    params.methodName = 'getMyJoin'
    let res = await wx.cloud.callFunction({
      name: 'prizeApi',
      data: params
    })
    
    return res.result.list
  },
  async getMyWin(params){ 
    params.methodName = 'getMyWin'
    let res = await wx.cloud.callFunction({
      name: 'prizeApi',
      data: params
    })
    return res.result.list
  }
}